package com.sysu.minsysu.zdkb;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sysu.minsysu.zdkb.utils.Course;

public class RoomsStateView extends RelativeLayout {

	private static final String TAG = "RoomStateView";
	
	int mWidth = 480;
	int mHeight = 700;
	int mTopBarHeight;
	int mLeftBarWidth;
	int mRowHeight;
	int mColWidth;

	int mFloorCount = 5;
	int mLongestFloorLength = 6;
	// 存储教室信息，-1为无此教室， 0为空闲，1为正在使用
	int[][] mClassRoomState = new int[5][];

	ArrayList<Course> mCourseList;
	int mStartTime;
	int mEndTime;
	String mBuildingName;

	private static final int ROOM_COLOR_DISABLE = Color.DKGRAY;
	private static final int ROOM_COLOR_FREE = 0xff34c27e;
	private static final int ROOM_COLOR_INUSE = 0xffb5e61d;
	private static final int ROOM_COLOR_BARS = 0xff38aacb;
	
	private static final int ROOM_INFO_TEXTSIZE = 12;
	
	Paint mPaint = new Paint();
	
	ArrayList<RoomView>[] mRoomViewsList ;
	BackgroundView mBackgroundView;
	

	public RoomsStateView(Context context, int[] time, ArrayList<Course> courses, String buingingName, int[] roomInfo) {
		super(context);
		//setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		mCourseList = courses;
		mStartTime = time[0];
		mEndTime = time[1];
		mBuildingName = buingingName;
		if (roomInfo != null)
			setClassRoomNumInFloor(roomInfo);
		
		setupViews();
		setupCourseState();
	}
	
	private void setupViews(){
		mBackgroundView = new BackgroundView(getContext());
		addView(mBackgroundView, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		
		mRoomViewsList = new ArrayList[mFloorCount];
		for( int i=0; i<mRoomViewsList.length; i++ )
			mRoomViewsList[i] = new ArrayList<RoomView>();
		
		for( int i=0; i<mClassRoomState.length; i++ ){
			for( int j=0; j<mClassRoomState[i].length; j++ ){
				if( mClassRoomState[i][j]!=-1){
					RoomView view = new RoomView(getContext(), i*100+j);
					mRoomViewsList[i].add(view);
					addView(view, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				}
			}
		}
		
	}
	
	private void setupCourseState(){
		if( mCourseList==null || mCourseList.size()==0 )
			return;
		
		for( Course course : mCourseList ){
			int room = course.getRoom();
			int floor = room/100-1;
			int num = room%100-1;
			mRoomViewsList[floor].get(num).addCourse(course);
		}
	}

	public RoomsStateView(Context context, ArrayList<Course> courses, int[][] roomInfo) {
		super(context);
		setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		setClassRoomState(roomInfo);
	}

	public void setFloorNum(int n) {
		mFloorCount = n;
	}

	public void setClassRoomState(int[][] state) {
		if (state != null) {
			mClassRoomState = state;
			mFloorCount = mClassRoomState.length;
		}
	}

	public void setClassRoomNumInFloor(int[] floorLen) {
		mClassRoomState = new int[floorLen.length][];
		mLongestFloorLength = 0;
		mFloorCount = floorLen.length;
		for (int i = 0; i < floorLen.length; i++)
			mLongestFloorLength = floorLen[i] > mLongestFloorLength ? floorLen[i]
					: mLongestFloorLength;

		for (int i = 0; i < mClassRoomState.length; i++) {
			mClassRoomState[i] = new int[mLongestFloorLength];
		}

		for (int i = 0; i < floorLen.length; i++) {
			int j = 0;
			for (j = 0; j < floorLen[i]; j++)
				mClassRoomState[i][j] = 0;
			while (j < mLongestFloorLength)
				mClassRoomState[i][j++] = -1;
		}
	}

	@Override
	public void onLayout(boolean changed, int left, int top, int right, int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		mWidth = right - left;
		mHeight = bottom - top;
		calculateWidgetSize();
		
		for( int i=0; i<mRoomViewsList.length; i++ ){
			for( int j=0; j<mRoomViewsList[i].size(); j++ ){
				RoomView view = mRoomViewsList[i].get(j);
				view.layout(mLeftBarWidth+j*mColWidth+1, mTopBarHeight+mRowHeight*i+1, mLeftBarWidth+(j+1)*mColWidth-1, mTopBarHeight+mRowHeight*(i+1)-1 );
			}
		}
	}

	private void calculateWidgetSize() {
		mTopBarHeight = 40;
		mLeftBarWidth = 45;
		mRowHeight = (mHeight - mTopBarHeight) / mFloorCount;
		mColWidth = (mWidth - mLeftBarWidth) / mLongestFloorLength;
	}

	private class BackgroundView extends View {

		private static final int TOPBARTEXTSIZE = 25;
		private static final int LEFTBARTEXTSIZE = 23;

		public BackgroundView(Context context) {
			super(context);
		}

		@Override
		protected void onMeasure(int width, int height) {
			setMeasuredDimension(mWidth, mHeight);
		}

		@Override
		public void onDraw(Canvas canvas) {
			drawBackground(canvas);
			drawTopBar(canvas);
			drawLeftBar(canvas);
			drawLines(canvas);
		}

		private void drawBackground(Canvas canvas) {
			mPaint.setColor( ROOM_COLOR_DISABLE );
			canvas.drawColor( ROOM_COLOR_BARS );
			canvas.drawRect(mLeftBarWidth, mTopBarHeight, mWidth, mHeight, mPaint);
		}

		private void drawTopBar(Canvas canvas) {
			mPaint.setColor(Color.BLACK);
			mPaint.setTextSize(TOPBARTEXTSIZE);
			canvas.drawText(mBuildingName, 15, TOPBARTEXTSIZE, mPaint);
//			for (int i = 0; i < mLongestFloorLength; i++) {
//				canvas.drawText("" + (i+1), mLeftBarWidth
//						+ (int) (mColWidth * (i + 0.4)), TOPBARTEXTSIZE + 4, mPaint);
//			}
		}

		private void drawLeftBar(Canvas canvas) {
			int spec = (int) (mTopBarHeight + mRowHeight * 0.6);
			mPaint.setTextSize(LEFTBARTEXTSIZE);
			for (int i = 0; i < mFloorCount; i++)
				canvas.drawText((i+1) + "F", (int) (mLeftBarWidth * 0.2), spec
						+ mRowHeight*i, mPaint);
		}

		private void drawLines(Canvas canvas) {
			mPaint.setColor(Color.BLACK);
			mPaint.setStrokeWidth(2);
			for (int i = 0; i < mFloorCount; i++)
				canvas.drawLine(0, mTopBarHeight + mRowHeight * i, mWidth, mTopBarHeight
						+ mRowHeight * i, mPaint);

			for (int i = 0; i < mLongestFloorLength; i++)
				canvas.drawLine(mLeftBarWidth + mColWidth * i, mTopBarHeight, mLeftBarWidth
						+ mColWidth * i, mHeight, mPaint);
		}
	}
	
	private class RoomView extends TextView{

		private ArrayList<Course> mCourses=new ArrayList<Course>();
		private int mRoomNum;
		private int mFloorNum;
		private int mNumInFloor;
		
		public RoomView(Context context, int roomNum) {
			super(context);
			mFloorNum = roomNum/100;
			mNumInFloor = roomNum%100;
			mRoomNum = (mFloorNum+1)*100 + mNumInFloor+1;
			
			setLayoutParams( new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			setBackgroundColor(ROOM_COLOR_FREE);
			mClassRoomState[mFloorNum][mNumInFloor] = 0;
			setGravity(Gravity.CENTER);
			setTextSize(10);
			setTextColor(Color.BLACK);
		}
		
		@Override
		protected void onMeasure( int width, int height ){
			width = mColWidth;
			height = mRowHeight;
			setMeasuredDimension(width, height);
		}
		
		public void addCourse( Course course ){
			mCourses.add(course);
			String text="";
			for( Course c : mCourses )
				text += (c.getCourseName() + "\n" + c.getTimeStart() + "-" + (c.getTimeStart()+c.getClassCount()-1) + "节\n");
			//Log.d( TAG, text);
			setText( text );
			setBackgroundColor(ROOM_COLOR_INUSE);
			mClassRoomState[mFloorNum][mNumInFloor] = 1;
		}
		@Override
		protected void onDraw( Canvas canvas ) {
			super.onDraw(canvas);
			mPaint.setColor(Color.BLACK);
			mPaint.setTextSize(ROOM_INFO_TEXTSIZE);
			canvas.drawText(""+mRoomNum, 2, ROOM_INFO_TEXTSIZE, mPaint);
		}
		
		
		
	}
	
}
